package cn.itcast.servlet;

import cn.itcast.domain.Admin;
import cn.itcast.domain.User;
import cn.itcast.service.AdminService;
import cn.itcast.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
    private AdminService service = new AdminServiceImpl();

    // TODO: 2020年7月14日20:15:22  登录功能
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //调用service,
        Admin admin = service.login(username, password);
        System.out.println(admin);
        if (admin != null) {//用户登录成功,跳到index.jsp页面

            HttpSession session1 = req.getSession();
            session1.setAttribute("admin", admin);
            resp.sendRedirect(req.getContextPath()+"/index.jsp");
        } else {//登录
            HttpSession session = req.getSession();
            session.setAttribute("msg", "用户名或者密码错误,长点心啊");
            resp.sendRedirect(req.getContextPath() + "/login.jsp");
        }

    }
}
